package com.onetech.adrewards.free.dialogs;

import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.activities.BaseNewActivity;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.http.APIRequestResult;
import com.onetech.adrewards.free.models.GiftCardModel;
import com.onetech.adrewards.free.objects.RegisterSuccessDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.todddavies.components.progressbar.ProgressWheel;
import com.zuzu.dialogs.BaseDialog;

public class ExchangeFinishedDialog extends BaseDialog {
	
	
	private TextView mExchangeTxt=null;
	private TextView mExpireDateTxt=null;
	private View mMainView=null;
	private View mOkBtn=null;
	private ProgressWheel mProgressBar = null;
	private LinearLayout mExchangerContainer = null;
	private LinearLayout mlnlProcessBar = null; 
	private int mType=0;
	private TextView mMsgHeader=null;
	
	private long mCurrentTime = 0;
	private boolean isSending=false;
	
	private View mNotFoundData=null;
	private View mWaitingForLoading=null;
	protected View mBtnRetry=null;
	protected ProgressWheel mProgress=null;
	private ExchangeProcess mExchangeProcess=null;
	
	
	
	RegisterSuccessDTO mResult=null;
	
	private int buttonPressId=-1; 
	
	
	
	private Tracker mTracker;
	private Activity mActivity=null;
	
	
	public ExchangeFinishedDialog(Activity context, int _type){
		super(context, context.getString(R.string.exchange_finished),
				TYPE_NO_BUTTON, R.layout.dialog_exchange_finished_layout);
		mActivity=context;
		this.mType =_type;
		//showMyPoints(false);
		initView();
		doExchangeNow();
		initListener();
		
		
		setOnDialogListener(new OnDialogListener() {

			@Override
			public void onPositiveButtonClicked() {
			//	((BaseNewActivity) mActivity).getUserCoins();
				dismiss();
			}

			@Override
			public void onNegativeButtonClicked() {
				//((BaseNewActivity) mActivity).getUserCoins();
				dismiss();
			}

			@Override
			public void onDefaultButtonClicked() {
				//((BaseNewActivity) mActivity).getUserCoins();
				dismiss();
			}
		});
		
	}	
	private void initView() {
		mExchangeTxt=(TextView)findViewById(R.id.msg);	
		mExpireDateTxt=(TextView)findViewById(R.id.expire_date);	
		mMsgHeader=(TextView)findViewById(R.id.msgHeader);	
		
		
		
		
		
		mOkBtn=(LinearLayout)findViewById(R.id.btnOk);
		mExchangerContainer =(LinearLayout)findViewById(R.id.exchangeContainer);
		
		mMainView=(LinearLayout)findViewById(R.id.mainContainer);
		mlnlProcessBar = (LinearLayout)findViewById(R.id.lnlProgressBar);
		mProgressBar=(ProgressWheel)findViewById(R.id.splashProgressBar);
		mProgressBar.setText("");
		mProgressBar.setContourColor(Color.TRANSPARENT);
		mProgressBar.spin();
		mProgressBar.setVisibility(View.INVISIBLE);
		initNotFoundAndLoadingView();
		showNotFoundOrLoadingView(false, false);
		
	}
	private void initListener() {
		mOkBtn.setOnClickListener(this);
	}
	private void doExchangeNow(){
		if(mExchangeProcess==null || mExchangeProcess.getStatus()==Status.FINISHED){
			mExchangeProcess=new ExchangeProcess();
			mExchangeProcess.execute();
		}
	}
	protected void initNotFoundAndLoadingView(){
		mNotFoundData=(LinearLayout) findViewById(R.id.notFoundDataContainer);		
		mWaitingForLoading=(LinearLayout) findViewById(R.id.waitingLoading);
		mProgress=(ProgressWheel)findViewById(R.id.progressWheel);
		mProgress.spin();
		showNotFoundOrLoadingView(false, true);
		mBtnRetry=(LinearLayout)findViewById(R.id.btnRetry);
		
	}
	protected void showNotFoundOrLoadingView(boolean isShowNotFound,boolean isShowLoading){
		if(isShowNotFound){
			mNotFoundData.setVisibility(View.VISIBLE);
		}else{
			mNotFoundData.setVisibility(View.GONE);
		}
		if(isShowLoading){
			mWaitingForLoading.setVisibility(View.VISIBLE);
		}else{
			mWaitingForLoading.setVisibility(View.GONE);
		}
	}
	
	private void showHideProgressBar(boolean show){
		if(show){
			mProgressBar.setVisibility(View.VISIBLE);
			mExchangerContainer.setVisibility(View.GONE);
			mlnlProcessBar.setVisibility(View.VISIBLE);
		}
		else{
			mProgressBar.setVisibility(View.GONE);
			mExchangerContainer.setVisibility(View.VISIBLE);
			mlnlProcessBar.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.btnOk)
		{
			//((BaseNewActivity) mActivity).getUserCoins();
			((BaseNewActivity)mActivity).getUserInfo();
			
			dismiss();
			
		}
		else {
			super.onClick(v);
		}
	}
	
	private class ExchangeProcess extends
	AsyncTask<Void, Void, APIRequestResult> {
@Override
protected void onPreExecute() {		
	super.onPreExecute();
	showHideProgressBar(true);
}
@Override
protected APIRequestResult doInBackground(Void... params) {
	try {
		Bundle mParams=new Bundle();
		mParams.putString("mem_id", AppConfig.uId);
		mParams.putString("card_type", String.valueOf(mType));
		return GiftCardModel.getInstance().exchangeProcess(mParams);
	} catch (Exception e) {
		return null;
	}
}

@Override
protected void onPostExecute(APIRequestResult result) {
	super.onPostExecute(result);
	if(result!=null){
		try {
			if (result.error_code.trim().equals("0") && result.data != null && !result.data.equals("")) {
				JSONObject jb=(JSONObject)result.data;
				String giftCode="コード： " + jb.getJSONObject("result").getString("gift_card");
				mExchangeTxt.setText(giftCode);
				mExpireDateTxt.setText("有効期限： "+ jb.getJSONObject("result").getString("expire_date"));
				((BaseNewActivity) mActivity).getUserCoins();
			}else{
				String error_message=result.error_msg;
				AdrewadsUtils.showInfoDialog(getContext(), "", error_message);
				dismiss();
			}					
		} catch (Exception e) {
			AdrewadsUtils.showInfoDialog(getContext(), "", getContext().getString(R.string.exchange_failed_error_message));
			dismiss();
		}
	}else{
		AdrewadsUtils.showInfoDialog(getContext(), "", getContext().getString(R.string.exchange_failed_error_message));
		dismiss();
	}
	showHideProgressBar(false);
}
}
@Override
public void onStart() {
super.onStart();
try {
    mTracker.set(Fields.SCREEN_NAME, mActivity.getString(R.string.ExchangeProcessDialog));            
    mTracker.send(MapBuilder.createAppView().build());            
} catch (Exception e) {
}
}
}
